<?php

// 创建数据库连接
function get_db_conn() {
  static $conn = null;
  if($conn) return $conn;
  global $config;
  $conn = mysql_connect($config['db_host'], $config['db_user'], $config['db_pass']);
  mysql_select_db($config['db_name'], $conn);
  if(mysql_get_server_info() > '4.1') {
    mysql_query('SET NAMES "utf8"', $conn);
  }
  return $conn;
}

// 发送鲜花
function do_send($from, $to, $flower, $message) {
  global $my;
  global $config;
  // 写数据库
  $conn = get_db_conn();
  $time = time();
  $message = mysql_real_escape_string($message);
  $sql = "INSERT INTO `".$config['db_prefix']."flowers` ( `id` , `from` , `to` , `time` , `flower` , `message` )
          VALUES (NULL , '$from', '$to', $time, $flower, '$message');";
  if(!mysql_query($sql, $conn)) {
    return false;
  }

  // 发动态

  try{
    $method="publishTemplatizedAction";
    $params['template_id']=2;
    $params['body_data']='{"liwu":"' . $config['flowers'][$flower] . '", "touser":"<yk:name uid=\''.$to.'\' linked=\'true\' />", "game":"<a href=\'http://apps.guanxi.koubei.com/php_songli/\'>礼尚往来</a>"}';
    $feed=$my->api_client->feed($method,$params);	
    //print_R($feed);
  }catch(Exception $ex){		
    //exit();
  }

  // 发通知
  
  $notification_info = '<yk:name uid="'.$from.'" linked="true" /> 通过 <a href="http://apps.guanxi.koubei.com/php_songli/">礼尚往来</a> 给你送了 ' . $config['flowers'][$flower] . '！';

  try{
    $method="send";
    $params['to_ids']="$to";
    $params['notification']="$notification_info";
    $send=$my->api_client->notifications($method,$params);	
    //print_R($send);
  }catch(Exception $ex){		
    //exit();
  }
  
  return true;
}

// 消息框
function message_box($title, $content = '', $type = 'success') {
  if ($type=='success') {
    $str ='            <div style="background-color: #ffc; padding: 0.7em 0.7em 0.7em 0.7em; border-bottom: 1px solid #e8d249; border-left: 1px solid #e8d249; border-right: 1px solid #e8d249; border-top: 1px solid #e8d249; margin: 0.7em 0.7em 0.7em 0.7em;">'."\n";
    $str.='            <h1 style="font-size: 1em;">'."\n";
    $str.='              '.$title."\n";
    $str.='            </h1>'."\n";
    $str.='            <p style="font-size: 12px;">'."\n";
    $str.='              '.$content."\n";
    $str.='            </p>'."\n";
    $str.='          </div>';  
  } elseif ($type=='error') {
    $str ='          <div style="background-color: #ffebe8; padding: 0.7em 0.7em 0.7em 0.7em; border-bottom: 1px solid #dd3c10; border-left: 1px solid #dd3c10; border-right: 1px solid #dd3c10; border-top: 1px solid #dd3c10; margin: 0.7em 0.7em 0.7em 0.7em;">'."\n";
    $str.='            <h1 style="font-size: 1em;">'."\n";
    $str.='              '.$title."\n";
    $str.='            </h1>'."\n";
    $str.='            <p style="font-size: 12px;">'."\n";
    $str.='              '.$content."\n";
    $str.='            </p>'."\n";
    $str.='          </div>';
  } else {
    $str ='            <div style="background-color: #ffc; padding: 0.7em 0.7em 0.7em 0.7em; border-bottom: 1px solid #e8d249; border-left: 1px solid #e8d249; border-right: 1px solid #e8d249; border-top: 1px solid #e8d249; margin: 0.7em 0.7em 0.7em 0.7em;">'."\n";
    $str.='            <h1 style="font-size: 1em;">'."\n";
    $str.='              '.$title."\n";
    $str.='            </h1>'."\n";
    $str.='            <p style="font-size: 12px;">'."\n";
    $str.='              '.$content."\n";
    $str.='            </p>'."\n";
    $str.='          </div>';     
  }
  return $str;
}

// 从数据库读取鲜花信息
function get_flowers($from = null, $to = null, $offset = 0, $limit = 30) {
  global $config;
  $conn = get_db_conn();
  if($from) {
    $sql = "SELECT * FROM `".$config['db_prefix']."flowers` WHERE `from` = '$from' ORDER BY `time` DESC LIMIT $offset , $limit";
  } elseif ($to) {
    $sql = "SELECT * FROM `".$config['db_prefix']."flowers` WHERE `to` = '$to' ORDER BY `time` DESC LIMIT $offset , $limit";
  } else {
    return false;
  }

  if(!$result = mysql_query($sql, $conn)) {
    return false;
  }

  $data = array();
  while ($row = mysql_fetch_assoc($result)) {
    $data[] = $row;
  }
  mysql_free_result($result);
  return $data;
}
